package com.sy.olddriver.utils;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;

/**
 * 验证码工具类
 * @author Administrator
 */
public class ValidateCodeUtils {
    private ValidateCodeUtils() {

    }

    public static String getRandomStr(int length) {
        String str = "3456789abcdefghjkmnpqrstuvwxy";
        String result = "";
        Random r = new Random();
        for (int i = 1; i <= length; i++) {
            result += str.charAt(r.nextInt(str.length()));
        }
        return result;
    }

    @SuppressWarnings("all")
    public static Map<String, Object> drawString() {
        String code = getRandomStr(4);
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("code", code);

        Random r = new Random();
        BufferedImage image = new BufferedImage(120, 30, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
        g.fillRect(0, 0, 120, 30);
        g.setFont(new Font("", Font.BOLD, 30));
        g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
        g.drawString(code, 20, 20);
        for (int i = 1; i < 15; i++) {
            g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
            g.drawLine(r.nextInt(120), r.nextInt(30), r.nextInt(120), r.nextInt(30));
        }
        map.put("image", image);
        return map;
    }
}
